"""
Case Type   : Operator
Case Name   : 实现操作符的函数只有一个参数，存在参数类型，缺少restrict，join 参数，合理报错
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1、创建实现操作符的函数
    2、创建操作符,存在左参数类型，缺少restrict，join 参数
    3、创建操作符，存在右参数类型，缺少restrict，join 参数
    3、清理环境
Expect      :
    1、成功
    2、创建失败
    3、创建失败
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class OpreatorTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('dbuser')
        self.user_node = Node('dbuser')
        self.constant = Constant()

    def test_dolphin(self):
        text = '----step1:创建实现操作符的函数；expect：创建成功----'
        self.log.info(text)
        sql_cmd = f'drop function if exists test_procedure() cascade;'\
                  f'create function test_procedure(a int) return bool as\n'\
                  f'begin\n'\
                  f'if !a then return t;\n'\
                  f'else return f;\n'\
                  f'end if;\n'\
                  f'end;\n'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('DROP FUNCTION', sql_res, '执行失败' + text)
        self.assertIn('CREATE FUNCTION', sql_res, '执行失败' + text)

        text = '----step2:创建操作符,存在左参数类型，缺少restrict，join 参数;expect:创建失败----'
        self.log.info(text)
        sql_cmd1 = f'create operator &&&&('\
                   f'leftarg = int,'\
                   f'procedure = test_procedure,'\
                   f'commutator = &&&&,'\
                   f'negator = !&&&&,'\
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("ERROR", sql_res1,'执行失败' + text)

        text = '----step3:创建操作符，存在右参数类型，缺少restrict，join 参数;expect:创建失败----'
        self.log.info(text)
        sql_cmd2 = f'create operator @@#%%(' \
                   f'rightarg = int,' \
                   f'procedure = test_procedure,' \
                   f'commutator = @@#%%,' \
                   f'negator = !@@#%%,' \
                   f'hashes, merges);'
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2)
        self.log.info(sql_res2)
        self.assertIn("ERROR", sql_res2, '执行失败' + text)

    def tearDown(self):
        text = '--step4:清理环境;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop function test_procedure() cascade;"
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn("DROP FUNCTION", sql_res, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
